package fun.xiefeihong.video.list.utils

import io.lettuce.core.RedisClient
import org.slf4j.Logger
import org.slf4j.LoggerFactory

class RedisUtils {

    static final Logger log = LoggerFactory.getLogger(RedisUtils.class)

    static boolean isConnect(String uri){
        def client
        def connect
        def isConnect = false
        try{
            client = RedisClient.create(uri)      // connection, 线程安全的长连接，连接丢失时会自动重连，直到调用 close 关闭连接。
            connect = client.connect()
            def commands = connect.sync()       // sync, 默认超时时间为 60s.
            commands.set("host", uri)
            def value = commands.get("host")
            if(value == uri){
                isConnect = true
            }
        } catch(e) {
            log.warn(e.message)
        } finally {
            if(connect){
                connect.close()
            }
            if(client){
                client.shutdown()
            }
            isConnect
        }
    }

}
